# 测试Redis认证的PowerShell脚本
Write-Host "Testing Redis authentication..." -ForegroundColor Yellow

# 创建TCP连接测试函数
function Test-RedisAuth {
    param(
        [string]$Password = $null
    )
    
    try {
        $socket = New-Object System.Net.Sockets.TcpClient
        $socket.Connect("127.0.0.1", 6379)
        
        if ($socket.Connected) {
            Write-Host "✓ Connected to Redis successfully" -ForegroundColor Green
            
            $stream = $socket.GetStream()
            
            if ($Password) {
                # 尝试认证
                $authCommand = "*2`r`n`$4`r`nAUTH`r`n`$" + $Password.Length + "`r`n" + $Password + "`r`n"
                $authBytes = [System.Text.Encoding]::ASCII.GetBytes($authCommand)
                $stream.Write($authBytes, 0, $authBytes.Length)
                
                # 读取认证响应
                $buffer = New-Object byte[] 1024
                $response = $stream.Read($buffer, 0, 1024)
                $responseText = [System.Text.Encoding]::ASCII.GetString($buffer, 0, $response)
                
                Write-Host "Auth response: $($responseText.Trim())" -ForegroundColor Cyan
                
                if ($responseText.Contains("+OK")) {
                    Write-Host "✓ Authentication successful with password: $Password" -ForegroundColor Green
                    return $true
                } else {
                    Write-Host "✗ Authentication failed with password: $Password" -ForegroundColor Red
                    Write-Host "Response: $($responseText.Trim())" -ForegroundColor Red
                    return $false
                }
            } else {
                # 不使用密码，直接发送PING
                $pingCommand = "*1`r`n`$4`r`nPING`r`n"
                $pingBytes = [System.Text.Encoding]::ASCII.GetBytes($pingCommand)
                $stream.Write($pingBytes, 0, $pingBytes.Length)
                
                # 读取PING响应
                $buffer = New-Object byte[] 1024
                $response = $stream.Read($buffer, 0, 1024)
                $responseText = [System.Text.Encoding]::ASCII.GetString($buffer, 0, $response)
                
                Write-Host "PING response without auth: $($responseText.Trim())" -ForegroundColor Cyan
                
                if ($responseText.Contains("+PONG")) {
                    Write-Host "✓ Redis accessible without authentication" -ForegroundColor Green
                    return $true
                } else {
                    Write-Host "✗ Redis requires authentication" -ForegroundColor Yellow
                    return $false
                }
            }
        }
    }
    catch {
        Write-Host "✗ Connection failed: $($_.Exception.Message)" -ForegroundColor Red
        return $false
    }
    finally {
        if ($socket) {
            $socket.Close()
        }
    }
}

# 测试不同的认证方式
Write-Host "`n1. Testing without password..." -ForegroundColor Yellow
$noAuthResult = Test-RedisAuth

Write-Host "`n2. Testing with password 'RedisData@123456.'..." -ForegroundColor Yellow
$authResult = Test-RedisAuth -Password "RedisData@123456."

Write-Host "`n3. Testing with empty password..." -ForegroundColor Yellow
$emptyAuthResult = Test-RedisAuth -Password ""

# 总结
Write-Host "`n=== Test Results ===" -ForegroundColor Magenta
Write-Host "No auth: $(if($noAuthResult){'SUCCESS'}else{'FAILED'})" -ForegroundColor $(if($noAuthResult){'Green'}else{'Red'})
Write-Host "With password: $(if($authResult){'SUCCESS'}else{'FAILED'})" -ForegroundColor $(if($authResult){'Green'}else{'Red'})
Write-Host "Empty password: $(if($emptyAuthResult){'SUCCESS'}else{'FAILED'})" -ForegroundColor $(if($emptyAuthResult){'Green'}else{'Red'}) 